package com.rst.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.Exam;
import com.rst.model.ExamGradingLevels;
import com.rst.model.ExamGroups;
import com.rst.model.ExamScore;
import com.rst.model.Section;
import com.rst.model.Student;
import com.rst.model.Subject;
import com.rst.service.ExamService;
import com.rst.service.SectionService;
import com.rst.service.StudentService;
import com.rst.service.SubjectService;


@Controller
public class ExamController {
	
	@Autowired
	ExamService examService;
	
	@Autowired
	SectionService sectionService;
	
	@Autowired
	SubjectService subjectService;

	@Autowired
	StudentService studentService;
		
	// exam grading level CRUD
	
	@RequestMapping(value="/createExamGradingLevel", method=RequestMethod.GET)
	public ModelAndView createExamGradingLevel()
	{
		ModelAndView mav = new ModelAndView("exam/gradingLevel/createExamGradingLevel");
		ExamGradingLevels examGradingLevels = new ExamGradingLevels();		
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("examGradingLevels", examGradingLevels);
		return mav;
	}
	
	@RequestMapping(value="/saveExamGradingLevels", method=RequestMethod.POST)
	public String saveExamGradingLevels(@ModelAttribute("examGradingLevels")ExamGradingLevels examGradingLevels, BindingResult result, SessionStatus status)
	{
		examService.saveExamGradingLevels(examGradingLevels);
		status.setComplete();
		return "redirect:manageExamGradingLevels.htm";
	}
	
	@RequestMapping(value="/editExamGradingLevels", method=RequestMethod.GET)
	public ModelAndView editExamGradingLevels(@RequestParam("id")Long examGradingLevelsId)
	{
		ModelAndView mav = new ModelAndView("exam/gradingLevel/editExamGradingLevel");
		ExamGradingLevels examGradingLevels = examService.getExamGradingLevelsById(examGradingLevelsId);
		List<Section> sectionList = sectionService.getSectionList();;
		mav.addObject("sectionList", sectionList);
		mav.addObject("examGradingLevels", examGradingLevels);
		return mav;
	}
	
	@RequestMapping(value="/manageExamGradingLevels", method=RequestMethod.GET)
	public ModelAndView manageExamGradingLevels()
	{
		ModelAndView mav = new ModelAndView("exam/gradingLevel/manageExamGradingLevels");
		List<ExamGradingLevels> examGradingLevelsList = examService.getExamGradingLevelsList();
		mav.addObject("examGradingLevelsList", examGradingLevelsList);
		return mav;
	}
	@RequestMapping(value="/deleteExamGradingLevels", method=RequestMethod.GET)
	public String deleteExamGradingLevels(@RequestParam("id")Long examGradingLevelsId)
	{
		String status = examService.deleteExamGradingLevels(examGradingLevelsId);
		return "redirect:manageExamGradingLevels.htm" ;
	}

	@RequestMapping(value="/updateExamGradingLevels", method=RequestMethod.POST)
	public String updateExamGradingLevels(@ModelAttribute("examGradingLevels") ExamGradingLevels examGradingLevels, SessionStatus status ,
			@RequestParam("id")Long examGradingLevelsId)
	{
		
		examService.updateExamGradingLevels(examGradingLevels , examGradingLevelsId);	
		status.setComplete();
		return "redirect:manageExamGradingLevels.htm";
	}
	
	
	
	

	// create exam group CRUD	
	
	@RequestMapping(value="/createExamGroups", method=RequestMethod.GET)
	public ModelAndView createExamGroups()
	{
		ModelAndView mav = new ModelAndView("exam/group/createExamGroups");
		ExamGroups examGroups = new ExamGroups();
		List<Section> sectionList = sectionService.getSectionList();
		mav.addObject("sectionList", sectionList);
		mav.getModelMap().put("examGroups", examGroups);
		return mav;
	}
	
	@RequestMapping(value="/saveExamGroups", method=RequestMethod.POST)
	public String saveExamGroups(@ModelAttribute("examGroups")ExamGroups examGroups, BindingResult result, SessionStatus status)
	{
		examService.saveExamGroups(examGroups);
		status.setComplete();
		return "redirect:manageExamGroups.htm";
	}
	
	@RequestMapping(value="/editExamGroups", method=RequestMethod.GET)
	public ModelAndView editExamGroups(@RequestParam("id")Long examGroupsId)
	{
		ModelAndView mav = new ModelAndView("exam/group/editExamGroups");
		ExamGroups examGroups = examService.getExamGroupsById(examGroupsId);
		mav.addObject("examGroups", examGroups);
		return mav;
	}
	
	@RequestMapping(value="/manageExamGroups", method=RequestMethod.GET)
	public ModelAndView manageExamGroups()
	{
		ModelAndView mav = new ModelAndView("exam/group/manageExamGroups"); 
		List<ExamGroups> examGroupsList = examService.getExamGroupsList();
		mav.addObject("examGroupsList", examGroupsList);
		return mav;
	}
	@RequestMapping(value="/deleteExamGroups", method=RequestMethod.GET)
	public String deleteExamGroups(@RequestParam("id")Long examGroupsId)
	{
		String status = examService.deleteExamGroups(examGroupsId);
		return "redirect:manageExamGroups.htm" ;
	}
	@RequestMapping(value="/updateExamGroups", method=RequestMethod.POST)
	public String updateExamGroups(@ModelAttribute("examGroups") ExamGroups examGroups, SessionStatus status ,
	@RequestParam("id")Long examGroupId)
	{
		
		examService.updateExamGroups(examGroups ,examGroupId);	
		status.setComplete();
		return "redirect:manageExamGroups.htm";
	}
	
	

	
	
	// create exam CRUD	
	
	@RequestMapping(value="/createExam", method=RequestMethod.GET)
	public ModelAndView createExam()
	{
		ModelAndView mav = new ModelAndView("exam/createExam");
		Exam exam = new Exam();
		mav.getModelMap().put("exam", exam);
		return mav;
	}
	
	@RequestMapping(value="/saveExam", method=RequestMethod.POST)
	public String saveExam(@ModelAttribute("exam")Exam exam, BindingResult result, SessionStatus status)
	{
		examService.saveExam(exam);
		status.setComplete();
		return "redirect:manageExam.htm";
	}
	
	@RequestMapping(value="/editExam", method=RequestMethod.GET)
	public ModelAndView editExam(@RequestParam("id")Long examId)
	{
		ModelAndView mav = new ModelAndView("exam/editExam");
		Exam exam = examService.getExamById(examId);
		mav.addObject("exam", exam);
		return mav;
	}
	
	@RequestMapping(value="/manageExam", method=RequestMethod.GET)
	public ModelAndView manageExam(@RequestParam("id")Long sectionId)
	{
		ModelAndView mav = new ModelAndView("exam/manageExam");
		List<Subject> subjectList = subjectService.getSubjectListBySectionId(sectionId);
		List<Exam> examList = examService.getExamList();		
		mav.addObject("examList", examList);
		mav.addObject("subjectList", subjectList);
		return mav;
	}
	@RequestMapping(value="/deleteExam", method=RequestMethod.GET)
	public String deleteExam(@RequestParam("id")Long examId)
	{
		String status = examService.deleteExam(examId);
		return "redirect:manageExam.htm" ;
	}
	@RequestMapping(value="/updateExam", method=RequestMethod.POST)
	public String updateExam(@ModelAttribute("exam") Exam exam, SessionStatus status,
	@RequestParam("id")Long examId)
	{		
		examService.updateExam(exam , examId);	
		status.setComplete();
		return "redirect:manageExam.htm?id="+exam.getSubjectId();
	}
	
	
	
	// enter exam score CRUD	
	
	@RequestMapping(value="/createExamScore", method=RequestMethod.GET)
	public ModelAndView createExamScore(@RequestParam("sectionId")Long sectionId)
	{
		ModelAndView mav = new ModelAndView("exam/score/createExamScore");
		ExamScore examScore = new ExamScore();
		List<Student> studentList = studentService.getStudentsBySectionId(sectionId);
		mav.addObject("studentList", studentList);
		mav.getModelMap().put("examScore", examScore);
		return mav;
	}
	
	@RequestMapping(value="/saveExamScore", method=RequestMethod.POST)
	public String saveExamScore(@ModelAttribute("examScore")ExamScore examScore , BindingResult result, SessionStatus status)
	{
		examService.saveExamScore(examScore);
		status.setComplete();
		return "redirect:manageExamScore.htm";
	}
	
	@RequestMapping(value="/editExamScore", method=RequestMethod.GET)
	public ModelAndView editExamScore(@RequestParam("id")Long examScoreId)
	{
		ModelAndView mav = new ModelAndView("exam/score/editExamScore");
		ExamScore examScore = examService.getExamScoreById(examScoreId);
		mav.addObject("examScore", examScore);
		return mav;
	}
	
	@RequestMapping(value="/manageExamScore", method=RequestMethod.GET)
	public ModelAndView manageExamScore(@RequestParam("id")Long sectionId , @RequestParam("examId")Long examId)
	{
		ModelAndView mav = new ModelAndView("exam/score/manageExamScore");
		List<ExamScore> examScoreList = examService.getExamScoreList(examId);
		mav.addObject("examScoreList", examScoreList);
		mav.addObject("sectionId", sectionId);
		return mav;
	}
	@RequestMapping(value="/deleteExamScore", method=RequestMethod.GET)
	public String deleteExamScore(@RequestParam("id")Long examScoreId)
	{
		String status = examService.deleteExamScore(examScoreId);
		return "redirect:manageExamScore.htm" ;
	}
	
	@RequestMapping(value="/updateExamScore", method=RequestMethod.POST)
	public String updateExamScore(@ModelAttribute("examScore") ExamScore examScore, SessionStatus status ,
	@RequestParam("id")Long examScoreId)
	{
		
		examService.updateExamScore(examScore,examScoreId);	
		status.setComplete();
		return "redirect:manageExamScore.htm";
	}
	
}
